Destructuring
Destructuring is a better way to assign local variables from expressions that return a table.
Array destructuring
Assuming we have this table:
pluto
local t = { 3, 6, 9 }
This is how we would assign each value to variables called a
, b
, and c
using the Lua and Pluto ways:
Lua waypluto
local a, b, c = table.unpack(t)
Pluto waypluto
local [a, b, c] = t
Table destructuring
Assuming we have this table:
pluto
local t = {name = "John",age = 42}
This is how we would assign each field to a variable of the same name using the Lua and Pluto ways:
Lua waypluto
local name = t.namelocal age = t.age
Pluto waypluto
local { name, age } = t
Different variable name
Assuming we have the same table as above, this is how we'd assign the name
and age
fields to n
and a
variables, respectively, using the Lua and Pluto ways:
Lua waypluto
local n = t.namelocal a = t.age
Pluto waypluto
local { n = name, a = age } = t
Standard Library
Table destructuring can be used to require multiple standard library modules at once using the '*' module:
pluto
local { base64, json } = require "*"